#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/2/2 21:41
# @Author  : Aquarius
# @File    : demo1.py
# @Software: PyCharm
import base64
import re

import requests

# res = requests.get('https://www.baidu.com')
# print(res.text)
# res = requests.post('http://httpbin.org/post', data={'key' : 'value'})
# params = {'name':'libai','age':['18','19']}
# res = requests.get('http://httpbin.org/get', params=params)
# http://httpbin.org/get?name=libai
# print(res.url)# http://httpbin.org/get?name=libai&age=18&age=19
# url = "https://www.baidu.com"
# headers = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"
# }
# res = requests.get(url, headers=headers)
# print(res)
# cookies = {
#     'cookies1':'class_37'
# }
# url = 'http://httpbin.org/cookies'
# res = requests.get(url,cookies=cookies)
# print(res.text)

# proxies = {
#     'http':'http://58.212.43.34:9999',
#     # 'https':'https://10.10.1.10:1080'
# }
# res = requests.get('https://www.baidu.com',proxies=proxies)
# print(res.text)
#
# res = requests.get('http://github.com', allow_redirects=False)
# print(res.status_code)  # 请求的页面状态码

# 张三的个人中心（个人信息、购票记录）url  ---》   李四请求，没有登陆的。重定向到登陆页面
# 商城价格，爬虫（登录操作，获取cookie）----》     看到商品价格

# 证书验证
# from requests.exceptions import
# res = requests.get('http://httpbin.org',verify = False)

# 超时
# res = requests.get("https://www.baidu.com",timeout=1)
# print(res)


# 响应请求
# 字符编码
# res = requests.get('http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%9B%BE%E7%89%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=&copyright=&word=%E5%9B%BE%E7%89%87&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=&fr=&expermode=&force=&pn=120&rn=30&gsm=&1580735063020=')
# res.encoding = 'utf-8'  # 指定编码格式
# print(res.text)
# 二进制
# 图片传输（编码）bases64。。。。
# print(res.content)
# json
# print(res.json())
# 状态码
# print(res.status_code)
# # cookie
# print(res.cookies)
# # headers
# print(res.headers['Connection'])
# # url
# print(res.url)


# 1、访问主页面              获取到当前的 cookies
# 2、验证码     验证码的请求（携带上一次cookies）
# 【成功，不成功】
import requests

# 1、主页面
login_url = "https://kyfw.12306.cn/otn/resources/login.html"
headers = {
    "Referer": "https://www.12306.cn/index/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"
}

session = requests.Session()
# 添加
session.headers.update(headers)

response = session.get(url=login_url)
# print(response.cookies)
# cookies = response.cookies
# 2、下载验证码
capt_url = "https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&1580737745071&callback=jQuery19103163659468210125_1580737674102&_=1580737674103"
capt_response = session.get(capt_url)
# cookies = capt_response.cookies
# print(capt_response.text)
# 获取图片的信息
img_data = re.findall(b'"image":"(.*?)"', capt_response.content)[0]
# print(img_data)  # 有base64编码
img_data = base64.b64decode(img_data)  # base64解码
# print(img_data)
with open('capt_img.jpg', 'wb') as f:
    f.write(img_data)
# 验证验证码
check_url = "https://kyfw.12306.cn/passport/captcha/captcha-check"
def get_index(indexs):
    # 选择图片坐标
    map = {
        '1': '40,43',
        '2': '18,43',
        '3': '180,43',
        '4': '258,43',
        '5': '37,117',
        '6': '103,117',
        '7': '178,117',
        '8': '250,117'
    }
    indexs = indexs.split(',')
    res = []
    for i in indexs:
        res.append(map[i])
    return ','.join(res)
data = {
    "callback": "jQuery19103163659468210125_1580737674102",
    "answer": get_index(input('请输入正确的图片（逗号分隔）：')),
    "rand": "sjrand",
    "login_site": "E",
    "_": "1580737674105"
}
check_res = session.get(
    check_url,
    params=data)
print(check_res.text)




